<?php

namespace App\Http\Controllers\user;

use App\Dao\ProjectDao;
use App\Dao\ShopsDao;
use App\Dao\VipOrderDao;
use App\Model\DouyinOrderModel;
use App\Services\Logger;
use App\Services\PayServices;
use EasyWeChat\Kernel\Exceptions\Exception;

class NoticeController extends BaseController
{

    /**
     * @throws Exception
     */
    public function notice_vip(): \Symfony\Component\HttpFoundation\Response
    {
        $result = PayServices::get_app()->handlePaidNotify(function ($message,$fail){

            VipOrderDao::changeVip($message['out_trade_no']);

        });
        return $result->send();
//        VipOrderDao::changeVip();
    }


    /**
     * @throws Exception
     */
    public function notice_project(): \Symfony\Component\HttpFoundation\Response
    {
        $result = PayServices::get_app()->handlePaidNotify(function ($message,$fail){

            ProjectDao::changeProject($message['out_trade_no']);

        });
        return $result->send();
    }




    public function event(){
        $all = request()->all();
        Logger::event("=====>event",$all);
        //商家授权回调
        if($all['event'] == 'life_partner_order_notify'){
            ShopsDao::updateStatus(json_decode($all['content'],true));
        }else if($all['event'] == 'life_trade_order_notify'){
            $datas = json_decode($all['content'],true);
            if($datas['action'] == 'pay_success'){
                Logger::event("=====>pay_success",$all);

                $order = $datas['order'];
                //购买
                DouyinOrderModel::query()->create([
                    'order_amount'=>$order['pay_amount'],
                    'order_account_id'=>$order['account_id'],
                    'status'=>1,
                    'order_sanfang_id'=>$order['order_id']
                ]);
            }

        }else if($all['event'] == 'life_trade_certificate_notify'){
            $datas = json_decode($all['content'],true);
            //推款
            if($datas['action'] == 'refund_success') {
                $order = $datas['certificate'];
                DouyinOrderModel::query()->where('order_sanfang_id',$order['order_id'])->update([
                    'status' => 4
                ]);
            }else if($datas['action'] == 'verify'){
                //核销
                $order = $datas['certificate'];
                DouyinOrderModel::query()->where('order_sanfang_id',$order['order_id'])->update([
                    'status' => 2
                ]);

                //进行返费
                DouyinOrderModel::changeData($order['order_id']);
            }
        }else{
            Logger::event("=====>event",$all);
        }
        return json_encode($all['content']);
    }


}
